package com.ukao.cashregister.pesenter;

import android.content.Context;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.AllMarkprintBean;
import com.ukao.cashregister.bean.ClothginQuiryBean;
import com.ukao.cashregister.bean.OrderStateBean;
import com.ukao.cashregister.bean.PayInfoBean;
import com.ukao.cashregister.bean.QueryPaBean;
import com.ukao.cashregister.bean.QuiryDetailBean;
import com.ukao.cashregister.bean.ReceiptBean;
import com.ukao.cashregister.bean.RefundMethodBean;
import com.ukao.cashregister.bean.SendModeBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.bean.RefundRecordBean;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.DecimalUtil;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.QuiryDetailView;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observable;

import static com.ukao.cashregister.R.id.amount;
import static com.ukao.cashregister.R.id.transNo;

/**
 * Created by ukaodev on 2017/6/26.
 */

public class QuiryDetailPesenter extends BasePresenter<QuiryDetailView> {

    public QuiryDetailPesenter(QuiryDetailView view) {
        attachView(view);
    }






    /**
     *订单查询详情
     */
    public void qrderQuiryDetailsData(Context  context,String  id  ) {

        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.qrderQuiryDetails(Constant.createParameter(mHashMap)),
                new ApiCallback<QuiryDetailBean>() {
                    @Override
                    public void onSuccess(QuiryDetailBean model) {

                        if (model.getHttpCode()==200){
                            mvpView.getDataSuccess(model.getData());
                        }else{
                             mvpView.loadFail(model.getMsg());
                        }


                   }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 图形进度条
     */
    public void orderStatusprogress(String  orderId) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.orderStatusprogress(Constant.createParameter(mHashMap)),
                new ApiCallback<OrderStateBean>() {
                    @Override
                    public void onSuccess(OrderStateBean model) {

                        if (model.getHttpCode()==200){
                            mvpView.orderStatusData(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *订单详情查询交易流水  支付方式
     */

    public void queryPayList( String  id) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.queryPayList(Constant.createParameter(mHashMap)),
                new ApiCallback<QueryPaBean>() {
                    @Override
                    public void onSuccess(QueryPaBean model) {

                        if (model.getHttpCode()==200){
                            mvpView.queryPayData(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }



    /**
     *检测订单是否支付
     */

    public void queryPayStatus( String  id) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.queryPayStatus(Constant.createParameter(mHashMap)),
                new ApiCallback<PayInfoBean>() {
                    @Override
                    public void onSuccess(PayInfoBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadPayStatus(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }



    /**
     *取衣
     */
    public void productSign(Context context, String  orderProIds) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderProIds",orderProIds);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.productSign(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                           mvpView.takesClothesSuccess(model.getMsg());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *订单签收
     */
    public void orderInfoSign(Context context, String orderId) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.orderInfoSign(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadSignSuccess(model.getMsg());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     * 订单完成
     */
    public void orderInfoCompleteid(Context context, String  orderId) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.orderInfoCompleteid(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadCompleteSuccess(model.getMsg());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     *打印订单
     */
    public void printReceipt(Context context, String  id ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.printReceipt(Constant.createParameter(mHashMap)),
                new ApiCallback<ReceiptBean>() {
                    @Override
                    public void onSuccess(ReceiptBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadReceipt(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *全打标
     */
    public void allprintWash(Context  context,final  String  orderId) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.allprintWash(Constant.createParameter(mHashMap)),
                new ApiCallback<AllMarkprintBean>() {
                    @Override
                    public void onSuccess(AllMarkprintBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.allmarKprintSucceed(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *获取送件方式
     */
    public void sendMode(Context  context    ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("code","ORDER_SEND_MODE");
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.keyList(Constant.createParameter(mHashMap)),
                new ApiCallback<SendModeBean>() {
                    @Override
                    public void onSuccess(SendModeBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.SendModeData((ArrayList<SendModeBean>) model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *修改送件模式
     */
    public void updateSendMode(Context  context,String  orderId ,String  sendMode, final  String   updateMsg) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("sendMode",sendMode); // 送件模式
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.updateSendMode(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.updateSendMode(model.getMsg(),updateMsg);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     批量返工
     */
    public void stockReworkBatch(String  proIds ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("proIds",proIds);  //衣物id列表
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.stockReworkBatch(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.stockReworkBatch();
                        }else{
                           mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     衣物 添加  返洗成功
     */
    public void BatchInReladd( Context mContext, String id ,final String orderProIds ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("inId",id);
        mHashMap.put("orderProIds", orderProIds);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        Observable Observable;
        Observable =apiStores.BatchInReladd(Constant.createParameter(mHashMap)) ;
        addSubscription(Observable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.retreatSuccess();

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 取衣详情
     */
    /**
     *获取衣物订单信息
     */
    public void clothginQuirydetail(Context  context,String phone   ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("scanCode",phone);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.clothginQuirydetail(Constant.createParameter(mHashMap)),
                new ApiCallback<ClothginQuiryBean>() {
                    @Override
                    public void onSuccess(ClothginQuiryBean model) {

                        if (model.getHttpCode()==200){
                            mvpView.clothingDetailsSuccess(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }
                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *订单作废
     */

    public void orderInfoDestroy(Context context, String  id,String remark) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("remark",remark);/*作废备注*/
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.orderInfoDestroy(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadOrderInfoDestroy(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *    衣物退款
     * @param context
     * @param id  订单
     * @param amount  退款金额
     * @param orderProIds  衣物ID
     * @param backeCause  退款原因
     * @param payMethod  退款方式

     */
    public void clothingRefund(Context context, String  id,String  amount,String orderProIds,String   backeCause,String  payMethod) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("amount",DecimalUtil.multiply(amount));
        mHashMap.put("orderProIds",orderProIds);/*衣物id*/
        mHashMap.put("remark",backeCause);
        if (!CheckUtils.isEmpty(payMethod)){   mHashMap.put("payMethod",payMethod );   }
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        L.i("mHashMap="+mHashMap.toString());
        addSubscription(apiStores.clothingRefund(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.refundSucceed(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 订单退款
     * @param context
     * @param orderId  订单id
     * @param amount  退款金额
     * @param remark  备注
     *  @param payMethod  退款方式
     */
    public void orderRefund(Context context, String  orderId,String amount,String  remark,String  payMethod) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("amount", DecimalUtil.multiply(amount));/*退款/修改金额*/
        mHashMap.put("remark",remark);/*remark 备注*/
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        if (!CheckUtils.isEmpty(payMethod)){   mHashMap.put("payMethod",payMethod );   }
        L.i("mHashMap="+mHashMap.toString());
        addSubscription(apiStores.orderRefund(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.refundSucceed(model.getData());

                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 退款列表
     */
    public void tradeOrderRefund(Context context,final String  orderId ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.tradeOrderRefund(Constant.createParameter(mHashMap)),
                new ApiCallback<RefundRecordBean>() {
                    @Override
                    public void onSuccess(RefundRecordBean model) {
                        if (model.getHttpCode()==200){

                            tradeAuditRefund(orderId,model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     * 审核退款列表
     */
    public void tradeAuditRefund( String  orderId,final RefundRecordBean  mRefundRecordBean ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.tradeAuditRefund(Constant.createParameter(mHashMap)),
                new ApiCallback<RefundRecordBean>() {
                    @Override
                    public void onSuccess(RefundRecordBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.refundRecordSuceed((ArrayList)mRefundRecordBean.getList(),(ArrayList)model.getData().getList());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     *

     修改工厂运单号
     * @param mOrderId
     * @param transNo  工厂运单号
     */
    public void updateFactoryTransNo(  String  mOrderId, final   String  transNo ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",mOrderId);/*订单衣物id*/
        mHashMap.put("transNo",transNo);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.updateFactoryTransNo(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.updateFactorySucceed(transNo);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 查询可以退款方式列表
     */
    public void refundMethodList( ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.refundMethodList(Constant.createParameter(mHashMap)),
                new ApiCallback<RefundMethodBean>() {
                    @Override
                    public void onSuccess(RefundMethodBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.refundMethodSucceed((ArrayList<RefundMethodBean>) model.getData());

                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }






}
